package ch.ethz.eai.handler;

import java.util.Iterator;

import org.apache.axiom.om.OMElement;
import org.apache.axiom.soap.SOAPBody;
import org.apache.axiom.soap.SOAPEnvelope;
import org.apache.axis2.AxisFault;
import org.apache.axis2.context.MessageContext;
import org.apache.axis2.handlers.AbstractHandler;

public class InHandler extends AbstractHandler {

	@Override
	public InvocationResponse invoke(MessageContext mc) throws AxisFault {
		SOAPEnvelope envelope = mc.getEnvelope();
		SOAPBody body = envelope.getBody();
		Iterator i = body.getFirstElement().getChildElements();
		while (i.hasNext())
		{
			OMElement el = (OMElement) i.next();
			if (el.getLocalName().equals("stype"))
			{
				String msg = el.getText().toLowerCase();
				if (msg.indexOf("clear") != -1)
					el.setText("0");
				else if (msg.indexOf("cloud") != -1)
					el.setText("1");
				else if (msg.indexOf("rain") != -1)
					el.setText("2");
				else if (msg.indexOf("storm") != -1)
					el.setText("3");
				else if (msg.indexOf("snow") != -1)
					el.setText("4");
				else
					return InvocationResponse.ABORT;
				break;
			}
		}
		return InvocationResponse.CONTINUE;
	}
}